﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Globalization;
using System.Threading;
using System.Windows.Data;
using System.Deployment.Application;

namespace WorkRecorder
{
    
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private CultureInfo currentCulture;

        public CultureInfo CurrentCulture
        {
            get { return currentCulture; }
            set { currentCulture = value; }
        }


        public string DeploymentPath { get; set; }


        public App()
        {
            CheckPath();
            //get default language
            string lang = ConfigurationManager.AppSettings.Get("defaultLanguage");
            CultureInfo ci = new CultureInfo(lang);
            Thread.CurrentThread.CurrentCulture = ci;
            Thread.CurrentThread.CurrentUICulture = ci;
            CurrentCulture = ci;

        }

        /// <summary>
        /// Check if network deployed (clickone) and setup path accordingly
        /// </summary>
        private void CheckPath()
        {
             
            //check if network deployed(clickonce)
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                DeploymentPath = ApplicationDeployment.CurrentDeployment.DataDirectory+"/";
            }
            else
            {

                DeploymentPath = string.Empty;
            }

        }


        /// <summary>
        /// Initilizae data sources for work types, theme names, and language names
        /// </summary>
        /// <param name="ci"></param>
        private void InitiliazeDataSources(CultureInfo ci)
        {
            var themeResource = this.FindResource("supportedThemesDataProvider") as XmlDataProvider;
            var languageResource = this.FindResource("supportedLanguagesDataProvider") as XmlDataProvider;
            var workTypesResource = this.FindResource("workTypesDataProvider") as XmlDataProvider;




            if (CurrentCulture.Name == "en-US")
            {
                themeResource.Source = new Uri(DeploymentPath+ConfigurationManager.AppSettings["supportedThemes"], UriKind.RelativeOrAbsolute);
                languageResource.Source = new Uri(DeploymentPath+ConfigurationManager.AppSettings["supportedLanguages"], UriKind.RelativeOrAbsolute);
                workTypesResource.Source = new Uri(DeploymentPath+ConfigurationManager.AppSettings["workTypes"], UriKind.RelativeOrAbsolute);

            }


            if (CurrentCulture.Name == "zh-CN")
            {
                themeResource.Source = new Uri(DeploymentPath+ConfigurationManager.AppSettings["supportedThemes-zh-CN"], UriKind.RelativeOrAbsolute);
                languageResource.Source = new Uri(DeploymentPath+ConfigurationManager.AppSettings["supportedLanguages-zh-CN"], UriKind.RelativeOrAbsolute);
                workTypesResource.Source = new Uri(DeploymentPath+ConfigurationManager.AppSettings["workTypes-zh-CN"], UriKind.RelativeOrAbsolute);
            }

 
        }

        /// <summary>
        /// Initilialize settings before UI loads
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Application_Startup(object sender, StartupEventArgs e)
        {            
            LoadTheme();
            //load xml data providers
            InitiliazeDataSources(Thread.CurrentThread.CurrentCulture);
        }

        /// <summary>
        /// Load selected theme 
        /// </summary>
        private void LoadTheme()
        {

            //if none for theme is selected, loads system depdenent theme
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var themePath = config.AppSettings.Settings["defaultTheme"].Value;
            //if string is nto null or set to default, load theme
            if ( !string.IsNullOrEmpty(themePath) && !themePath.Equals("default"))
            {
                Uri uri = new Uri(themePath, UriKind.RelativeOrAbsolute);
                //Uri uri = new Uri("Pack://application:,,,/WorkRecorder;Component/ConfigurationFiles/WPFThemes/ShinyRed.xaml", UriKind.Absolute);
                ResourceDictionary rd = new ResourceDictionary();
                rd.Source = uri;                 
                Resources.MergedDictionaries.Add(rd);

            }
        }

        void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show(this.FindResource("messageExitProgram").ToString(), this.FindResource("messageTitleWarning").ToString(), MessageBoxButton.YesNo);
            if (result == MessageBoxResult.No) { e.Cancel = true; }
            else if (result == MessageBoxResult.Cancel) { e.Cancel = false; }
        }



 
    }
}
